__doc__ = '''
Functions:
	------ Image Functions cv2 ------
	- findColor(img, color) -> Array
	- findColorZipped(img, color) -> Array
	- replaceColor(img, toReplace, replace) -> Image
	------ Game Functions ------
	- onTick(func, ms, *args) -> None
'''

## ------ Image Functions cv2 ------ ##
def findColor(img, color):
	import numpy as np 
	# import cv2
	'''
		Searches for color [B,G,R] in an numpy image

		returns:
		[numpy.array(x0,x1,...,xn), numpy.array(y0,y1,...,yn)]
	'''
	arr =  np.where(np.all(img == color, axis=-1))
	return arr


def findColorZipped(img, color):
	# import numpy as np 
	# import cv2
	'''
		Searches for color [B,G,R] in an numpy image

		returns:
		[(x0,y0),(x1,y1),...,(xn,yn)]
	'''
	arr = findColor(img, color)
	return zip(arr[0],arr[1])


def replaceColor(img, toReplace, replace):
	import numpy as np 
	# import cv2
	'''
		In <img>: Replaces color <toReplace> with <replace>

		returns new Image with replaced color
	'''
	newImg = np.copy(img)
	arr = findColorZipped(img, toReplace)

	for px in arr:
		newImg[px] = replace

	return newImg


## ------ For Games ------
def onTick(func, ms, *args):
	from thread import start_new_thread
	'''
		Starts new Thread with Gamelogic loop
	'''
	try:
		start_new_thread(_selfcallingFunc, (func, ms, args))
	except:
		print "Error: unable to start thread"

def _selfcallingFunc(func, ms, args):
	import time
	while True:
		func(*args)
		time.sleep(ms/1000.0)

## ------ For everything ------
acc = {}
def memo(f):
	global acc
	'''
		Memoize Decorizer
	'''
	def help(n):
		if n not in acc:
			acc[n] = f(n)
		return acc[n]
	return help
